﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace SuperX.Common.Helper
{
    /// <summary>
    /// 加密/解密帮助工具
    /// </summary>
    public class SafetyUtil
    {
        // 
        public static string DesEncrypt(string encryptString, string key, string iv)
        {
            string result;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] bytes2 = Encoding.UTF8.GetBytes(iv.Substring(0, 8));
                byte[] bytes3 = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateEncryptor(bytes, bytes2), CryptoStreamMode.Write);
                cryptoStream.Write(bytes3, 0, bytes3.Length);
                cryptoStream.FlushFinalBlock();
                StringBuilder stringBuilder = new StringBuilder();
                foreach (byte b in memoryStream.ToArray())
                {
                    stringBuilder.AppendFormat("{0:X2}", b);
                }
                result = stringBuilder.ToString();
            }
            catch
            {
                result = encryptString;
            }
            return result;
        }

        // 
        public static string DesDecrypt(string decryptString, string key, string iv)
        {
            string result;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] bytes2 = Encoding.UTF8.GetBytes(iv.Substring(0, 8));
                int num = decryptString.Length / 2;
                byte[] array = new byte[num];
                for (int i = 0; i < num; i++)
                {
                    string value = decryptString.Substring(i * 2, 2);
                    array[i] = Convert.ToByte(value, 16);
                }
                DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateDecryptor(bytes, bytes2), CryptoStreamMode.Write);
                cryptoStream.Write(array, 0, array.Length);
                cryptoStream.FlushFinalBlock();
                result = Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            catch
            {
                result = decryptString;
            }
            return result;
        }

        // 
        public static string EnFormatCrypt(string encrypted, int onegroupcount)
        {
            string text = string.Empty;
            string text2 = string.Empty;
            for (int i = 0; i < encrypted.Length; i++)
            {
                text2 += encrypted[i].ToString();
                if (text2.Length == onegroupcount || i == encrypted.Length - 1)
                {
                    text += text2;
                    if (i != encrypted.Length - 1)
                    {
                        text += "-";
                    }
                    text2 = string.Empty;
                }
            }
            return text;
        }

        // 
        public static string DeFormatCrypt(string formated)
        {
            string text = formated;
            while (text.IndexOf("-") >= 0)
            {
                text = text.Replace("-", "");
            }
            return text;
        }

        // 
        public static string MD5(string str, Encoding encoding)
        {
            return SafetyUtil.MD5(encoding.GetBytes(str));
        }

        // 
        public static string MD5(string fileName)
        {
            string result;
            using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] array = new byte[fileStream.Length];
                fileStream.Read(array, 0, array.Length);
                result = SafetyUtil.MD5(array);
            }
            return result;
        }

        //
        public static string MD5(byte[] data)
        {
            string text = "";
            byte[] array = System.Security.Cryptography.MD5.Create().ComputeHash(data);
            for (int i = 0; i < array.Length; i++)
            {
                text += array[i].ToString("X2");
            }
            return text;
        }

        // 
        public static string GetMd5Code(string text)
        {
            byte[] bytes = Encoding.Default.GetBytes(text);
            return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(bytes)).Replace("-", "");
        }
    }
}
